package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Vector;
import Negocio.Propietario;

public class AdmPersistenciaPropietario extends AdministradorPersistencia {
	
	private static AdmPersistenciaPropietario instancia;
	
	@Override
	public Long insertar(Object o) {
		System.out.println("LOG: AdmPersistenciaCerealCosechaFina / insert [INI]");
		try
		{
			Propietario a = (Propietario)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into propietario values (?,?)");
			
			//agregar campos
			
			s.setLong(1, a.getDni());
			s.setString(2, a.getNombre());
			s.execute();
			return a.getDni();
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
			return null;
		}
		
	}

	@Override
	public void modificar(Object o) {
		System.out.println("LOG: "+this.getClass().getName()+" / modificar [INI]");
		try
		{
			Propietario a = (Propietario)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  propietario set nombre = ? where dni = ?" );
			
			//agregar campos
			s.setString(1, a.getNombre());
			s.setLong(2, a.getDni());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: "+this.getClass().getName()+" / update [FIN]");
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("No se puede insertar el elemento");
		}

	}

	@Override
	public void borrar(Object d) {
		
		System.out.println("LOG: "+this.getClass().getName()+" / Delete [INI]");
		try
		{
			Propietario a = (Propietario)d;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cereal set estado = ? where dni = ?");
			s.setString(1, a.getNombre());
			s.setLong(2, a.getCodigo());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: "+this.getClass().getName()+" / Delete [FIN]");
		}
		catch (Exception e)
		{
			// TODO Exception
			System.out.println("No se puede elminiar el elemento");
		}
		

	}

	@Override
	public Vector<Object> buscarTodos() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object buscarPorCodigo(Object o) {
		System.out.println("LOG: "+this.getClass().getName()+" / findById [INI]");
		try
		{
			
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  propietario where dni = ?");			//agregar campos
			s.setLong(1,(Long)o);
			ResultSet result = s.executeQuery();
			Propietario a = null;
			
			if (result.next())
			{
				a = new Propietario(result.getString(2),result.getLong(1));
				
				
				
			} else
			{
				//TODO Exception
				System.out.println("No se encuentra el Cereal cosecha Gruesa");
				
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: "+this.getClass().getName()+" / findById [FIN] ");
			
			return a;
			
		}
		catch (Exception e)
		{	
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}
	}

	public static AdmPersistenciaPropietario getInstancia() {
		if (instancia == null)
			instancia = new AdmPersistenciaPropietario();
		return instancia;
	}
	
	private AdmPersistenciaPropietario (){}

	public Collection<Propietario> selectAll() {
		System.out.println("LOG: "+this.getClass().getName()+" / findById [INI]");
		try
		{
			
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  propietario");
			ResultSet result = s.executeQuery();
			Vector<Propietario> prop = new Vector<Propietario>();
			while (result.next())
			{
				prop.add(new Propietario(result.getString(2),result.getLong(1)));
	
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			System.out.println("LOG: "+this.getClass().getName()+" / findById [FIN] ");
			
			return prop;
			
		}
		catch (Exception e)
		{	
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}
	}

}
